package com.fight.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.fight.util.date.DateUtil;

public class ExportExcel {

    public static String getFileName() {
        return DateUtil.toString(new Date(), "yyyyMMdd-HHmmss");
    }

	public static XSSFWorkbook getWorkbook(List<?> resultList, String[] showName, String[] resourceField, Class<?> resultObj,
        Map<String, Map<String, String>> formatMap) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException {
    	XSSFWorkbook workbook = new XSSFWorkbook ();
    	XSSFSheet  sheet = workbook.createSheet("sheet1");
        sheet.setDefaultColumnWidth((short) 20);
        XSSFCellStyle centerStyle = workbook.createCellStyle();//设置为水平居中  
        centerStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);  
        centerStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); 
        XSSFRow row;
        XSSFCell cell;
        createTitle(showName, sheet);
        // 下面是输出各行的数据 下面是输出各行的数据 下面是输出各行的
        for (int i = 0,len=resultList.size(); i < len; i++) {
            Object result = resultList.get(i);
            row = sheet.createRow(i + 1);
            // 创建第 i+1 行
            for (int j = 0; j < resourceField.length; j++) {
                cell = row.createCell(j);// 创建第 j 列
                Method method;
                method = resultObj.getMethod(resourceField[j]);
                // 这里用到了反射机制，通 这里用到了反射机制， 这里用到了反射机制 过方法名来取得对应方法返回的结果对象
                Object obj = method.invoke(result);
                if (obj != null) {
                    if (formatMap != null && formatMap.containsKey(resourceField)) {
                        cell.setCellValue(formatMap.get(resourceField).get(obj.toString()));
                    } else {
                        cell.setCellValue(obj.toString());
                    }
                }
            }
        }
        return workbook;
    }

    private static void createTitle(String[] showName, XSSFSheet sheet) {
    	XSSFRow row = sheet.createRow(0); // 创建第 1 行，也就是输出表头 创建第
    	XSSFCell cell;
        for (int i = 0; i < showName.length; i++) {
            cell = row.createCell(i);
            // 创建第 i 列 创建第
            cell.setCellValue(new XSSFRichTextString(showName[i]));
        }
    }

   
    public static InputStream workbook2InputStream(XSSFWorkbook workbook, String fileName,HttpServletResponse response) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        workbook.write(baos);
        baos.flush();
        byte[] aa = baos.toByteArray();
        InputStream excelStream = new ByteArrayInputStream(aa, 0, aa.length);
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" +fileName+ ".xlsx");
        response.getOutputStream().write(aa);
        response.setContentLength(aa.length);
        response.getOutputStream().flush();
        baos.close();
        return excelStream;
        
    }
}
